package com.woniuxy.auth.service.impl;

import com.woniuxy.auth.service.ManagerService;
import com.woniuxy.auth.dao.RbacManagerDao;
import com.woniuxy.commons.entity.auth.RbacManager;
import com.woniuxy.commons.entity.auth.vo.RbacManagerVo;
import com.woniuxy.commons.result.Result;
import com.woniuxy.commons.utils.JWTUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <P> @Author: ideaAdmin
 * <P> TODO: Class
 * <P> TODO:
 * <P> Date: 2022/4/28:9:05
 * <P> Description:
 */
@Service
public class ManagerServiceImpl implements ManagerService {


    @Resource
    private RbacManagerDao rbacManagerDao;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Override
    public Result<Object> verity(RbacManagerVo rbacManagerVo,
                                 HttpServletResponse response) {

        if (rbacManagerVo.getAccount() == null ||
                rbacManagerVo.getPassword() == null) {
            return Result.fail().message("账号密码不能为空");
        }

        RbacManager account = rbacManagerDao.findByAccount(rbacManagerVo);

        if (account == null) {
            //TODO  响应结果
            return Result.fail().message("账号不存在");
        }

        if (!account.getPassword().equals(rbacManagerVo.getPassword())) {
            return Result.fail().message("密码错误");
        }


        //TODO  生成token 返回
        return generateToken(rbacManagerVo, response,account);

    }

    /**
     * <P>Author ideaAdmin
     * <P>TODO: 生成token
     * <P>Date 9:20 2022/4/28
     * <P>* @param rbacManagerVo
     *
     * @param response <P>* @return com.woniuxy.commons.result.Result<java.lang.Object>
     * @param account
     **/
    private Result generateToken(RbacManagerVo rbacManagerVo,
                                         HttpServletResponse response, RbacManager account) {
        try {
            //TODO  1.生成 token、refreshToken
            String token = JWTUtil.generateToken(rbacManagerVo.getAccount());
            String refreshToken = UUID.randomUUID().toString();

            //TODO 2.以refreshToken 作为key, token、账号为value放入redis
            Map<String, Object> map = new HashMap<>();

            map.put("token", token);
            map.put("uname", rbacManagerVo.getAccount());

            redisTemplate.opsForHash().putAll(refreshToken, map);
            // 设置过期时间 30天免登录
            redisTemplate.expire(refreshToken, 30, TimeUnit.DAYS);

            //TODO 3.设置响应头 返回token refreshToken
            response.setHeader("authorization", token);
            response.setHeader("refreshToken", refreshToken);
            //暴露设置的头
            response.setHeader("Access-Control-Expose-Headers", "authorization,refreshToken");

        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail().message("系统异常 稍后再试");
        }

        account.setPassword(null);
        return Result.ok(account).message("登录成功");

    }
}
